from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.shortcuts import get_object_or_404


from images.models import Image
from images.forms import ImageCreatedForm

# Create your views here.


@login_required
def image_create(request):
    if request.method == 'POST':
        form = ImageCreatedForm(data=request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            new_item = form.save(commit=False)
            new_item.user = request.user
            new_item.save()

            messages.success(request, 'Image added successfully')
            return redirect(new_item.get_absolute_url())
    else:
        form = ImageCreatedForm(data=request.GET)
    return render(request, 'images/image/create.html', {'section': 'images',
                                'form': form})


def image_detail(request, id, slug):
    image = get_object_or_404(Image, id=id, slug=slug)
    return render(request, 'images/image/detail.html',
                  {'section': 'images',
                  'image': image})


import redis
from django.conf import settings
r = redis.StrictRedis(host=settings.REDIS_HOST,
                      port=settings.REDIS_PORT,
                      db=settings.REDIS_DB)


def image_detail(request, id, slug):
    image = get_object_or_404(Image, id=id, slug=slug)